<template>
  <!-- 客户信息查询 v-if="roleId>=10 && roleId<=14" -->
  <Card>
    <div style="text-align:right">
      <Uploadv :action="updateUrl" :show-upload-list='false' :format="['pdf']" :multiple="true" name="multipartFile"
        :headers="headers" :before-upload="beforeUpload" :on-success="uploadSuccess" :on-error="uploadFail"
        style="display:inline-block">
        <Button type="primary" icon="ios-cloud-download-outline">导入文件</Button>
      </Uploadv>
    </div>

    <Table :columns="columns" :data="tableData" style="margin-top:15px">
      <template slot-scope="{ row }" slot="operate">
        <!-- 可删除操作人为登录人的文件，其他人上传无法上传，该按钮置灰 -->
        <span v-if="useragentcode == row.operatorNo" class="table-button" @click="handelDel(row)">删除</span>
        <span v-else class="table-button" style="color:#ccc">删除</span>
        <span class="table-button" @click="handelDownload(row)" style="margin-left: 10px;">下载并查看</span>
      </template>
    </Table>

    <Page :total="ipagination.total" show-sizer show-total style="text-align:right;margin-top:20px"
      @on-change="handleCurrentChange" @on-page-size-change="handleSizeChange"
      :page-size-opts="ipagination.pageSizeOptions" :current="ipagination.pageIndex" :page-size="ipagination.pageSize"
      transfer></Page>

  </Card>
</template>

<script>
import { exportFile, downloadUrl } from "../../util/util"
import { baseURL } from '../../util/url';
import { loadResourcesMixin } from "../../util/mixins/loadResourcesMixin";
import { tableChangeMixin } from "../../util/mixins/tableChangeMixin";
export default {
  name: "clientInfo",
  mixins: [loadResourcesMixin, tableChangeMixin],
  data() {
    return {
      mixinData: {
        tableAxios: "trainmanagementGetList",
      },
      updateUrl: "/scrm-gateway/scrm-bhuer/api/trainmanagement/upload",
      useragentcode: '',
      headers: {
        "Authorization": sessionStorage.getItem('credentials')
      },
      roleId: '',
      columns: [
        {
          title: "序号",
          type: "index",
          align: "center",
          width: 80
        },
        {
          title: "导入文件",
          key: "attachmentName",
          align: "center",
        },
        {
          title: "上传时间",
          key: "createTime",
          align: "center",
        },
        {
          title: "操作人",
          key: "operatorName",
          align: "center",
        },
        {
          title: "操作",
          key: "operate",
          slot: "operate",
          align: "center",
          width: "160",
          fixed: "right",
        },
      ],
    };
  },
  mounted() {
    // console.log('updateUrl', this.updateUrl);
    this.useragentcode = sessionStorage.getItem('useragentcode')
    this.roleId = sessionStorage.getItem('roleId')
  },
  methods: {
    handelDel(row) {
      this.$Modal.confirm({
        title: "确定要删除吗",
        onOk: () => {
          //点击确认的方法          
          this.apiB.trainmanagementDelete({ id: row.id, operatorNo: row.operatorNo }).then(res => {
            //console.log(res);
            if (res.code === 200) {
              this.$Message.success(res.msg);
              this.loadResources()
            } else {
              this.$Message.info(res.msg);
            }
          });
        },
      })
    },
    handelDownload(row) {
      // downloadUrl(row.url, row.attachmentName);  
      exportFile("/scrm-bhuer/api/trainmanagement/download?id=" + row.id)
    },
    beforeUpload(file) {
      let hz = file.name.split(".");
      let extra = hz[hz.length - 1];
      if (extra != "pdf") {
        this.$Message.info("请上传PDF格式的文件");
        return false;
      }
    },
    // 导入文件
    uploadSuccess(res) {
      this.$Message.info({
        content: res.msg,
        duration: 5
      })
      this.loadResources()
    },
    // 连不到后端的情况
    uploadFail(response, file, fileList) {
      this.$Message.info("保存失败");
    },
  },
};
</script>